using UnityEngine;
using UI.BankingPanel.Data;
using System.Collections.Generic;

/// <summary>
/// 粘贴板功能演示脚本
/// 展示导出时复制到粘贴板的功能
/// </summary>
public class ClipboardDemo : MonoBehaviour
{
    [Header("演示设置")]
    [SerializeField] private bool showClipboardContent = true;
    [SerializeField] private UnityEngine.UI.Text displayText;
    
    [Header("测试按钮")]
    [SerializeField] private UnityEngine.UI.Button testClipboardButton;
    [SerializeField] private UnityEngine.UI.Button showClipboardButton;
    [SerializeField] private UnityEngine.UI.Button clearClipboardButton;
    
    private void Start()
    {
        InitializeUI();
    }
    
    /// <summary>
    /// 初始化UI
    /// </summary>
    private void InitializeUI()
    {
        // 查找显示文本
        if (displayText == null)
        {
            var textObj = GameObject.Find("ClipboardDisplayText");
            if (textObj != null)
            {
                displayText = textObj.GetComponent<UnityEngine.UI.Text>();
            }
        }
        
        // 查找按钮并绑定事件
        if (testClipboardButton == null)
        {
            var buttonObj = GameObject.Find("TestClipboardButton");
            if (buttonObj != null)
            {
                testClipboardButton = buttonObj.GetComponent<UnityEngine.UI.Button>();
            }
        }
        
        if (showClipboardButton == null)
        {
            var buttonObj = GameObject.Find("ShowClipboardButton");
            if (buttonObj != null)
            {
                showClipboardButton = buttonObj.GetComponent<UnityEngine.UI.Button>();
            }
        }
        
        if (clearClipboardButton == null)
        {
            var buttonObj = GameObject.Find("ClearClipboardButton");
            if (buttonObj != null)
            {
                clearClipboardButton = buttonObj.GetComponent<UnityEngine.UI.Button>();
            }
        }
        
        // 绑定按钮事件
        if (testClipboardButton != null)
        {
            testClipboardButton.onClick.AddListener(TestClipboardExport);
        }
        
        if (showClipboardButton != null)
        {
            showClipboardButton.onClick.AddListener(ShowClipboardContent);
        }
        
        if (clearClipboardButton != null)
        {
            clearClipboardButton.onClick.AddListener(ClearClipboard);
        }
        
        UpdateDisplay("粘贴板功能演示已准备就绪\n点击按钮测试功能");
    }
    
    /// <summary>
    /// 测试粘贴板导出功能
    /// </summary>
    [ContextMenu("测试粘贴板导出")]
    public void TestClipboardExport()
    {
        UpdateDisplay("正在测试粘贴板导出功能...");
        
        try
        {
            // 创建测试数据
            var testData = CreateTestData();
            
            // 执行导出（启用粘贴板功能）
            var result = SimpleExcelExporter.ExportBankingDataToExcel(testData, "粘贴板测试", true);
            
            if (result.Success)
            {
                UpdateDisplay($"导出成功！\n{result.Message}\n\n点击'显示粘贴板内容'查看复制的数据");
                Debug.Log($"[ClipboardDemo] 导出成功，粘贴板内容已更新");
            }
            else
            {
                UpdateDisplay($"❌ 导出失败: {result.Message}");
                Debug.LogError($"[ClipboardDemo] 导出失败: {result.Message}");
            }
        }
        catch (System.Exception ex)
        {
            UpdateDisplay($"❌ 测试过程中发生错误: {ex.Message}");
            Debug.LogError($"[ClipboardDemo] 测试错误: {ex.Message}");
        }
    }
    
    /// <summary>
    /// 显示粘贴板内容
    /// </summary>
    [ContextMenu("显示粘贴板内容")]
    public void ShowClipboardContent()
    {
        try
        {
            string clipboardContent = GUIUtility.systemCopyBuffer;
            
            if (string.IsNullOrEmpty(clipboardContent))
            {
                UpdateDisplay("粘贴板为空");
            }
            else
            {
                UpdateDisplay($"=== 粘贴板内容 ===\n{clipboardContent}");
                Debug.Log($"[ClipboardDemo] 粘贴板内容长度: {clipboardContent.Length} 字符");
            }
        }
        catch (System.Exception ex)
        {
            UpdateDisplay($"❌ 读取粘贴板失败: {ex.Message}");
            Debug.LogError($"[ClipboardDemo] 读取粘贴板失败: {ex.Message}");
        }
    }
    
    /// <summary>
    /// 清空粘贴板
    /// </summary>
    [ContextMenu("清空粘贴板")]
    public void ClearClipboard()
    {
        try
        {
            GUIUtility.systemCopyBuffer = "";
            UpdateDisplay("✅ 粘贴板已清空");
            Debug.Log("[ClipboardDemo] 粘贴板已清空");
        }
        catch (System.Exception ex)
        {
            UpdateDisplay($"❌ 清空粘贴板失败: {ex.Message}");
            Debug.LogError($"[ClipboardDemo] 清空粘贴板失败: {ex.Message}");
        }
    }
    
    /// <summary>
    /// 创建测试数据
    /// </summary>
    private BankingData CreateTestData()
    {
        var bankingData = new BankingData();
        bankingData.odds = 42.0f;
        
        // 创建一些测试数据来展示粘贴板功能
        var testNumbers = new Dictionary<int, (float total, float eat, float report)>
        {
            // 按照用户要求的格式
            {1, (100f, 50f, 50f)},
            {2, (100f, 50f, 50f)},
            {3, (100f, 50f, 50f)},
            {5, (50f, 25f, 25f)},
            {6, (50f, 25f, 25f)},
            {8, (50f, 25f, 25f)},
            
            // 添加一些高风险号码来展示风险提醒
            {10, (500f, 400f, 100f)}, // 高风险
            {15, (300f, 250f, 50f)},  // 高风险
            {20, (200f, 150f, 50f)},
            {25, (150f, 100f, 50f)},
        };
        
        // 设置测试数据到BankingData
        var allData = bankingData.GetAllNumberBetData();
        foreach (var kvp in testNumbers)
        {
            var numberData = allData.Find(x => x.number == kvp.Key);
            if (numberData != null)
            {
                numberData.totalBetAmount = kvp.Value.total;
                numberData.selfBetAmount = kvp.Value.eat;
                numberData.reportAmount = kvp.Value.report;
                // 计算风险值（简化计算）
                numberData.riskValue = kvp.Value.eat * bankingData.odds - kvp.Value.total;
            }
        }
        
        Debug.Log($"[ClipboardDemo] 创建了包含 {testNumbers.Count} 个号码的测试数据");
        
        return bankingData;
    }
    
    /// <summary>
    /// 更新显示内容
    /// </summary>
    private void UpdateDisplay(string message)
    {
        if (displayText != null)
        {
            displayText.text = $"[{System.DateTime.Now:HH:mm:ss}] {message}";
        }
        
        Debug.Log($"[ClipboardDemo] {message}");
    }
    
    /// <summary>
    /// 复制自定义内容到粘贴板（测试用）
    /// </summary>
    [ContextMenu("复制测试文本")]
    public void CopyTestText()
    {
        string testText = "这是一个测试文本\n用于验证粘贴板功能\n时间: " + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        
        try
        {
            GUIUtility.systemCopyBuffer = testText;
            UpdateDisplay($"测试文本已复制到粘贴板\n内容: {testText}");
        }
        catch (System.Exception ex)
        {
            UpdateDisplay($"复制测试文本失败: {ex.Message}");
        }
    }
    
    private void OnDestroy()
    {
        // 清理事件监听
        if (testClipboardButton != null)
        {
            testClipboardButton.onClick.RemoveListener(TestClipboardExport);
        }
        
        if (showClipboardButton != null)
        {
            showClipboardButton.onClick.RemoveListener(ShowClipboardContent);
        }
        
        if (clearClipboardButton != null)
        {
            clearClipboardButton.onClick.RemoveListener(ClearClipboard);
        }
    }
} 